package util;

import com.nanosl.lib.db.Manager;
import ent.Bank;
import ent.Customer;
import ent.Employee;
import ent.EmployeePosition;
import ent.Item;
import ent.ItemType;
import ent.LastCode;
import ent.Operator;
import ent.RootArea;
import ent.Stock;
import ent.Supplier;
import ent.Town;
import ent.ViewPanel;
import java.util.Date;
import ui.CompanyView;
import ui.CustomerView;
import ui.DamageNotesView;
import ui.DamageStockView;
import ui.EmployeeView;
import ui.ItemView;
import ui.PurchaseInvoicePaymentView;
import ui.PurchaseInvoiceView;
import ui.SaleInvoicePaymentView;
import ui.SaleInvoiceView;
import ui.SelingPriceChangeView;
import ui.StockChangeView;
import ui.StockTransferView;
import ui.StockView;
import ui.SubDealerView;
import ui.SupplierView;
import ui.operator.OperatorView;
import ui.operator.PermissionView;
import ui.rep.RepPriceChangeView;
import ui.rep.RepRemainingStockView;
import ui.rep.RepReturnView;
import ui.rep.RepSaleView;
import ui.rep.RepStockChangeView;
import ui.rep.RepStockView;
import ui.report.AveragePriceView;
import ui.report.CollectionReportView;
import ui.report.IssuedChequeReportView;
import ui.report.PurchaseReportView;
import ui.report.ReceivedChequeReportView;
import ui.report.RepSaleReportView;
import ui.report.SalesReportView;
import ui.report.StockChangeReportView;
import ui.report.TransferReportView;
import ui.tool.BankView;
import ui.tool.EmployeePositionView;
import ui.tool.MenuManageView;
import ui.tool.RootAreaView;
import ui.tool.TownView;

/**
 *
 * @author t
 */
public class Init {

    private static Manager man = Manager.getInstance();

    public static void first() {
        employeePosition();
        employee();
        operator();
        rootArea();
        town();
        bank();
        supplier();
        customer();
        item();
        lastCode();
    }

    public static void operator() {
        if (man.count(Employee.class) == 0) {
            employee();
        }
        Employee employee = man.find(Employee.class, "000");
        Operator operator = new Operator("admin");
        operator.setPassword("n");
        operator.setCreatedDate(new Date());
        operator.setPower(0);
        operator.setViewPanelCollection(man.find(ViewPanel.class));
        operator.setEmployee(employee);
        man.update(operator);
    }

    private static void employee() {
        if (man.count(EmployeePosition.class) == 0) {
            employeePosition();
        }
        EmployeePosition employeePosition = man.find(EmployeePosition.class, "ADMIN");
        Employee employee = new Employee("000");
        employee.setFirstName("ADMINISTRATOR");
        employee.setLastName("NANO CREATIONS");
        employee.setAddressNumber("911");
        employee.setAddressStreet("TEST STREET");
        employee.setCity("POLONNARUWA");
        employee.setMobile("0724666666");
        employee.setFixedLine("0275673493");
        employee.setNotes("TEST NOTES");
        employee.setEmployeePosition(employeePosition);
        man.update(employee);
    }

    private static void employeePosition() {
        EmployeePosition employeePosition = new EmployeePosition("ADMIN");
        employeePosition.setDescription("ADMINISTRATOR");
        man.update(employeePosition);
    }

    private static void town() {
        Town town = new Town("000");
        town.setName("TEST TOWN");
        town.setRootArea(new RootArea("000"));
        man.update(town);
    }

    private static void bank() {
        Bank bank = new Bank("000");
        bank.setName("TEST BANK");
        man.update(bank);
    }

    private static void supplier() {
        Supplier supplier = new Supplier("000");
        supplier.setName("TEST SUPPLIER");
        supplier.setAddressNumber("101");
        supplier.setAddressStreet("TEST STREET");
        supplier.setCity("TEST CITY");
        supplier.setMobile("0000000000");
        supplier.setFixedLine("0000000001");
        supplier.setFax("0000000002");
        supplier.setNotes("TEST NOTES");
        supplier.setTown(man.find(Town.class, "000"));
        man.update(supplier);
    }

    private static void customer() {
        Customer customer = new Customer("000");
        customer.setName("TEST CUSTOMER");
        customer.setAddressNumber("100");
        customer.setAddressStreet("TEST STREET");
        customer.setCity("TEST CITY");
        customer.setMobile("0000000000");
        customer.setFixedLine("0000000001");
        customer.setFax("0000000002");
        customer.setNotes("TEST NOTES");
        man.update(customer);
    }

    private static void item() {
        Item item = new Item("000");
        item.setDescription("TEST ITEM");
        item.setSupplier(man.find(Supplier.class, "000"));
        ItemType itemType = new ItemType("001");
        item.setItemTypeType(itemType);
        Stock stock = new Stock(item.getCode());
        stock.setBundles(0.0);
        stock.setItem(item);
        stock.setQuantity(0.0);
        item.setStock(stock);
        man.update(itemType);
        man.update(item);
        man.update(stock);
    }

    private static void lastCode() {
        LastCode lastCode = new LastCode("SupplierInvoice");
        lastCode.setCode("000");
        man.update(lastCode);
    }

    private static void rootArea() {
        RootArea rootArea = new RootArea("000");
        rootArea.setName("DEFAULT");
        man.update(rootArea);
    }

    public static void views() {
        new AveragePriceView();
        new BankView();
        new CompanyView();
        new CustomerView();
        new DamageNotesView();
        new DamageStockView();
        new EmployeePositionView();
        new EmployeeView();
        new ItemView();
        new OperatorView();
        new CollectionReportView();
        new PermissionView();
        new SaleInvoiceView();
        new SelingPriceChangeView();
        new PurchaseInvoicePaymentView();
        new PurchaseInvoiceView();
        new PurchaseReportView();
        new RepPriceChangeView();
        new RepRemainingStockView();
        new RepReturnView();
        new RepSaleReportView();
        new RepSaleView();
        new RepStockChangeView();
        new RepStockView();
        new RootAreaView();
        new SaleInvoicePaymentView();
        new SaleInvoiceView();
        new SalesReportView();
        new StockChangeReportView();
        new StockChangeView();
        new StockTransferView();
        new StockView();
        new SubDealerView();
        new SupplierView();
        new TownView();
        new TransferReportView();
        new ReceivedChequeReportView();
        new MenuManageView();
        new IssuedChequeReportView();
        System.gc();
    }
//    public static void load() {
//        try {
//            DBM.getEMF().getProperties();
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(null, "DATABASE SERVER NOT CONNECTED!", "SERVER OFFLINE", JOptionPane.ERROR_MESSAGE);
//            System.exit(0);
//        }
//        Map<String, Object> map = 
//        Set<String> set = map.keySet();
//        for (Iterator<String> it = set.iterator(); it.hasNext();) {
//            String string = it.next();
//            System.out.println(string + " = " + map.get(string));
//        }
//    }
}
